/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.momarketing.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.momarketing.dao.OnlineSignupConfigDao;
import com.boxin.ims.modules.momarketing.entity.OnlineSignupConfig;
import com.boxin.ims.modules.wechat.utils.AssertUtils;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;

/**
 * 在线预约Service
 * @author Jakemanse
 * @version 2013-09-10
 */
@Component
@Transactional(readOnly = true)
public class OnlineSignupConfigService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(OnlineSignupConfigService.class);
	
	@Autowired
	private OnlineSignupConfigDao onlineSignupConfigDao;
	
	public OnlineSignupConfig get(Long id) {
		return onlineSignupConfigDao.findOne(id);
	}
	
	
	public OnlineSignupConfig getByUserId(Long userId,Long cid) {
		
		DetachedCriteria dc = onlineSignupConfigDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		if (AssertUtils.notNull(cid)) {
			dc.add(Restrictions.eq("id", cid));
		}
		List<OnlineSignupConfig> list =  onlineSignupConfigDao.find(dc);
		if(list != null){
			return (list.size() == 1) ? list.get(0) : null;
		}
		return null;
	}
	
   public List<OnlineSignupConfig> getByUser(Long userId) {
		
		DetachedCriteria dc = onlineSignupConfigDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		List<OnlineSignupConfig> list =  onlineSignupConfigDao.find(dc);
		return list;
	}
	
public OnlineSignupConfig getByCId(Long cid) {
		
		DetachedCriteria dc = onlineSignupConfigDao.createDetachedCriteria();
		if (AssertUtils.notNull(cid)) {
			dc.add(Restrictions.eq("id", cid));
		}
		List<OnlineSignupConfig> list =  onlineSignupConfigDao.find(dc);
		if(list != null){
			return (list.size() == 1) ? list.get(0) : null;
		}
		return null;
	}
	
	public Page<OnlineSignupConfig> find(Page<OnlineSignupConfig> page, OnlineSignupConfig onlineSignupConfig) {
		DetachedCriteria dc = onlineSignupConfigDao.createDetachedCriteria();
		if (onlineSignupConfig.getUser()!=null && onlineSignupConfig.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", onlineSignupConfig.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(onlineSignupConfig.getName())){
			dc.add(Restrictions.like("name", "%"+onlineSignupConfig.getName()+"%"));
		}
		if (StringUtils.isNotEmpty(onlineSignupConfig.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+onlineSignupConfig.getRemarks()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", OnlineSignupConfig.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return onlineSignupConfigDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(OnlineSignupConfig onlineSignupConfig) {
		onlineSignupConfigDao.save(onlineSignupConfig);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		onlineSignupConfigDao.deleteById(id);
	}
	
}
